// **************************************************************
// Com ++ Framework by the C++ World
// Please report defects: mailto:compp@cplusplusworld.com
// Please visit: http://www.cplusplusworld.com/socketlibrary.html
// **************************************************************

#include <iostream>
#include <string>
#include <cstdlib>
#include "ComPlusPlus/ComPlusPlus"
 
int main (int argc, char *argv [] ) {

 std::string str,FNC =  "/tmp/Client";
 std::string FNS =  "/tmp/Server";
 std::string string2besent= "Hello World";
 std::string string2bereceived;

try {
  std::cout << "Unix Socket Test: Stream Mode\n";
  // -> Comment 1
  compp::SocketUnix Client (FNC, compp::STREAM);	
  // -> Comment 2
  compp::SocketUnix *S, Server (FNS, compp::STREAM);	

  Server.Listen();
  Client.Connect  ( FNS );
  S = Server.Accept (); 
  Client.SetReadAhead ( true );
  S->SetReadAhead ( true );

  Client.Write ( "1234567890" );
  S->Read ( str, 5 ) ;
  std::cout << "1. received " << str << "\n";

  Client.Write ( "ABCDEFGH" ) ;
  S->Read ( str, 5 ) ;
  std::cout << "2. received " << str << "\n";

  S->Read ( str, 8 ) ;
  std::cout << "3. received " << str << "\n";

} catch ( syspp::ComException e ) {
	std::cout <<  "Exception " << e.what() << "\n" ;	
}


try {
  std::string from;
  char c [65536];
  memset ( c, 0, sizeof ( c ));
  FNC="DGRAMC";
  FNS="DGRAMS";
  std::cout << "Unix Socket Test: Datagram Mode\n";
  // -> Comment 1
  compp::SocketUnix Client (FNC, compp::DATAGRAM);	
  // -> Comment 2
  compp::SocketUnix Server (FNS, compp::DATAGRAM);	

  Client.Sendto ( "Hello", 5, FNS );
  Server.Recvfrom ( c, 65536, 0, from );

  std::cout << "Unix Socket Test: received string " << c << "from " << from << "\n";

} catch ( syspp::ComException e ) {
	std::cout <<  "Exception " << e.what() << "\n" ;	
}

 return 0;	
}

